package com.dianping.cat.home.heavy.transform;

import com.dianping.cat.home.heavy.IEntity;
import com.dianping.cat.home.heavy.entity.HeavyCache;
import com.dianping.cat.home.heavy.entity.HeavyCall;
import com.dianping.cat.home.heavy.entity.HeavyReport;
import com.dianping.cat.home.heavy.entity.HeavySql;
import com.dianping.cat.home.heavy.entity.Service;
import com.dianping.cat.home.heavy.entity.Url;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;







public class DefaultSaxParser
  extends DefaultHandler
{
  private DefaultLinker m_linker = new DefaultLinker(true);

/*  38 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  40 */   private Stack<String> m_tags = new Stack();

/*  42 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  46 */   private StringBuilder m_text = new StringBuilder();


/*  49 */   public static HeavyReport parse(InputStream in) throws SAXException, IOException { return (HeavyReport)parseEntity(HeavyReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  53 */   public static HeavyReport parse(Reader reader) throws SAXException, IOException { return (HeavyReport)parseEntity(HeavyReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  57 */   public static HeavyReport parse(String xml) throws SAXException, IOException { return (HeavyReport)parseEntity(HeavyReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  63 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  64 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  66 */       factory.setValidating(false);
/*  67 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  69 */       factory.newSAXParser().parse(is, handler);
/*  70 */       return (T)handler.getEntity();
/*  71 */     } catch (ParserConfigurationException e) {
/*  72 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  77 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  81 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  87 */     if (obj instanceof String) {
/*  88 */       String str = (String)obj;

/*  90 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  91 */         return (T)str.substring(1);
      }
/*  93 */       return obj;
    }
    if (obj instanceof InputStream) {
/*  96 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  98 */       in.mark(3);

/* 100 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 101 */         in.reset();
      }

/* 104 */       return (T)in;
/* 105 */     }  if (obj instanceof Reader) {
/* 106 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 108 */       in.mark(1);

/* 110 */       if (in.read() != 65279) {
/* 111 */         in.reset();
      }

/* 114 */       return (T)in;
    }
/* 116 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 122 */     if (value == null || value.length() == 0) {
/* 123 */       return defaultValue;
    }

/* 126 */     if (type == Boolean.class)
/* 127 */       return (T)Boolean.valueOf(value); 
/* 128 */     if (type == Integer.class)
/* 129 */       return (T)Integer.valueOf(value); 
/* 130 */     if (type == Long.class)
/* 131 */       return (T)Long.valueOf(value); 
/* 132 */     if (type == Short.class)
/* 133 */       return (T)Short.valueOf(value); 
/* 134 */     if (type == Float.class)
/* 135 */       return (T)Float.valueOf(value); 
/* 136 */     if (type == Double.class)
/* 137 */       return (T)Double.valueOf(value); 
/* 138 */     if (type == Byte.class)
/* 139 */       return (T)Byte.valueOf(value); 
/* 140 */     if (type == Character.class) {
/* 141 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 143 */     return (T)value;
  }




/* 149 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 154 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 159 */     if (uri == null || uri.length() == 0) {
/* 160 */       this.m_objs.pop();
/* 161 */       this.m_tags.pop();
    }


/* 165 */     this.m_text.setLength(0);
  }


/* 169 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 173 */   protected String getText() { return this.m_text.toString(); }


  private void parseForHeavyCache(HeavyCache parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 177 */     if ("url".equals(qName)) {
/* 178 */       Url url = this.m_maker.buildUrl(attributes);

/* 180 */       this.m_linker.onUrl(parentObj, url);
/* 181 */       this.m_objs.push(url);
/* 182 */     } else if ("service".equals(qName)) {
/* 183 */       Service service = this.m_maker.buildService(attributes);

/* 185 */       this.m_linker.onService(parentObj, service);
/* 186 */       this.m_objs.push(service);
    } else {
/* 188 */       throw new SAXException(String.format("Element(%s) is not expected under heavy-cache!", new Object[] { qName }));
    }

/* 191 */     this.m_tags.push(qName);
  }

  private void parseForHeavyCall(HeavyCall parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 195 */     if ("url".equals(qName)) {
/* 196 */       Url url = this.m_maker.buildUrl(attributes);

/* 198 */       this.m_linker.onUrl(parentObj, url);
/* 199 */       this.m_objs.push(url);
/* 200 */     } else if ("service".equals(qName)) {
/* 201 */       Service service = this.m_maker.buildService(attributes);

/* 203 */       this.m_linker.onService(parentObj, service);
/* 204 */       this.m_objs.push(service);
    } else {
/* 206 */       throw new SAXException(String.format("Element(%s) is not expected under heavy-call!", new Object[] { qName }));
    }

/* 209 */     this.m_tags.push(qName);
  }

  private void parseForHeavyReport(HeavyReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 213 */     if ("heavy-sql".equals(qName)) {
/* 214 */       HeavySql heavySql = this.m_maker.buildHeavySql(attributes);

/* 216 */       this.m_linker.onHeavySql(parentObj, heavySql);
/* 217 */       this.m_objs.push(heavySql);
/* 218 */     } else if ("heavy-call".equals(qName)) {
/* 219 */       HeavyCall heavyCall = this.m_maker.buildHeavyCall(attributes);

/* 221 */       this.m_linker.onHeavyCall(parentObj, heavyCall);
/* 222 */       this.m_objs.push(heavyCall);
/* 223 */     } else if ("heavy-cache".equals(qName)) {
/* 224 */       HeavyCache heavyCache = this.m_maker.buildHeavyCache(attributes);

/* 226 */       this.m_linker.onHeavyCache(parentObj, heavyCache);
/* 227 */       this.m_objs.push(heavyCache);
    } else {
/* 229 */       throw new SAXException(String.format("Element(%s) is not expected under heavy-report!", new Object[] { qName }));
    }

/* 232 */     this.m_tags.push(qName);
  }

  private void parseForHeavySql(HeavySql parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 236 */     if ("url".equals(qName)) {
/* 237 */       Url url = this.m_maker.buildUrl(attributes);

/* 239 */       this.m_linker.onUrl(parentObj, url);
/* 240 */       this.m_objs.push(url);
/* 241 */     } else if ("service".equals(qName)) {
/* 242 */       Service service = this.m_maker.buildService(attributes);

/* 244 */       this.m_linker.onService(parentObj, service);
/* 245 */       this.m_objs.push(service);
    } else {
/* 247 */       throw new SAXException(String.format("Element(%s) is not expected under heavy-sql!", new Object[] { qName }));
    }

/* 250 */     this.m_tags.push(qName);
  }

  private void parseForService(Service parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 254 */     this.m_objs.push(parentObj);
/* 255 */     this.m_tags.push(qName);
  }

  private void parseForUrl(Url parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 259 */     this.m_objs.push(parentObj);
/* 260 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 264 */     if ("heavy-report".equals(qName)) {
/* 265 */       HeavyReport heavyReport = this.m_maker.buildHeavyReport(attributes);

/* 267 */       this.m_entity = heavyReport;
/* 268 */       this.m_objs.push(heavyReport);
/* 269 */       this.m_tags.push(qName);
/* 270 */     } else if ("heavy-sql".equals(qName)) {
/* 271 */       HeavySql heavySql = this.m_maker.buildHeavySql(attributes);

/* 273 */       this.m_entity = heavySql;
/* 274 */       this.m_objs.push(heavySql);
/* 275 */       this.m_tags.push(qName);
/* 276 */     } else if ("url".equals(qName)) {
/* 277 */       Url url = this.m_maker.buildUrl(attributes);

/* 279 */       this.m_entity = url;
/* 280 */       this.m_objs.push(url);
/* 281 */       this.m_tags.push(qName);
/* 282 */     } else if ("service".equals(qName)) {
/* 283 */       Service service = this.m_maker.buildService(attributes);

/* 285 */       this.m_entity = service;
/* 286 */       this.m_objs.push(service);
/* 287 */       this.m_tags.push(qName);
/* 288 */     } else if ("heavy-call".equals(qName)) {
/* 289 */       HeavyCall heavyCall = this.m_maker.buildHeavyCall(attributes);

/* 291 */       this.m_entity = heavyCall;
/* 292 */       this.m_objs.push(heavyCall);
/* 293 */       this.m_tags.push(qName);
/* 294 */     } else if ("heavy-cache".equals(qName)) {
/* 295 */       HeavyCache heavyCache = this.m_maker.buildHeavyCache(attributes);

/* 297 */       this.m_entity = heavyCache;
/* 298 */       this.m_objs.push(heavyCache);
/* 299 */       this.m_tags.push(qName);
    } else {
/* 301 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 307 */     if (uri == null || uri.length() == 0) {
/* 308 */       if (this.m_objs.isEmpty()) {
/* 309 */         parseRoot(qName, attributes);
      } else {
/* 311 */         Object parent = this.m_objs.peek();
/* 312 */         String tag = (String)this.m_tags.peek();

/* 314 */         if (parent instanceof HeavyReport) {
/* 315 */           parseForHeavyReport((HeavyReport)parent, tag, qName, attributes);
/* 316 */         } else if (parent instanceof HeavySql) {
/* 317 */           parseForHeavySql((HeavySql)parent, tag, qName, attributes);
/* 318 */         } else if (parent instanceof Url) {
/* 319 */           parseForUrl((Url)parent, tag, qName, attributes);
/* 320 */         } else if (parent instanceof Service) {
/* 321 */           parseForService((Service)parent, tag, qName, attributes);
/* 322 */         } else if (parent instanceof HeavyCall) {
/* 323 */           parseForHeavyCall((HeavyCall)parent, tag, qName, attributes);
/* 324 */         } else if (parent instanceof HeavyCache) {
/* 325 */           parseForHeavyCache((HeavyCache)parent, tag, qName, attributes);
        } else {
/* 327 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 331 */       this.m_text.setLength(0);
    } else {
/* 333 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 339 */       return (new SimpleDateFormat(format)).parse(str);
/* 340 */     } catch (ParseException e) {
/* 341 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/heavy/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */